package com.rabbit.videoplayer.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.constraintlayout.widget.ConstraintLayout
import coil.load
import com.rabbit.videoplayer.listener.OnVideoStatusListener
import com.rabbit.videoplayer.R

class CoverView(context: Context, attrs: AttributeSet?=null): ConstraintLayout(context, attrs), OnVideoStatusListener {

    private val ivCover: ImageView = ImageView(context)

    init {
        val params = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
        addView(ivCover, params)
        ivCover.setBackgroundResource(R.drawable.bg_wb_mix_vod_cover)
        ivCover.scaleType = ImageView.ScaleType.FIT_CENTER
    }


    private fun hide(){
        if (this.visibility != View.GONE) {
            this.visibility = View.GONE
        }
    }


    fun show(){
//        if (this.visibility != View.VISIBLE) {
//            this.visibility = View.VISIBLE
//        }
    }

    /**
     * 设置封面图
     */
    fun setCoverBackgroundResource(@DrawableRes bgResId: Int){
        ivCover.setBackgroundResource(bgResId)
    }

    fun setCoverUrl(url: String){
        ivCover.setBackgroundColor(Color.BLACK)
        ivCover.load(url) {
            crossfade(300)
        }
    }


    override fun onVodInit(vararg init: Any?): Boolean {
        show()
        return false
    }

    override fun onVodPrepared(vararg prepared: Any) {

    }

    override fun onVodStart() {
        hide()
    }

    override fun onVodFirstFrameStart() {
        hide()
    }

    override fun onVodSwitchDisplayControlView() {
        
    }

    override fun onVodPause() {
        
    }

    override fun onVodStop() {
        show()
    }

    override fun onVodError(vararg params: Any) {
        show()
    }
    override fun onVodComplete() {

    }

    override fun onVodRelease() {

    }

    override fun onVodLockedScreen(vararg params: Any) {
        
    }

    override fun onVodFullScreen(vararg params: Any) {

    }

    override fun onVodLoadingBegin() {
    }

    override fun onVodLoadingProgress(vararg params: Any) {

    }

    override fun onVodLoadingEnd() {
    }

    override fun onVodBackPress(): Boolean {
        return false
    }

    override fun onEpisodeSelected(){
    }

    override fun onVodShowUnlockUI(isDisplayUI: Boolean) {
        if (isDisplayUI) {
            show()
        }
    }

    override fun onVodSkits() {

    }
}